package com.dt76.smallloanbackenduser.controller.etao;

import com.dt76.smallloanbackenduser.remote.etao.LoginRemote;
import com.dt76.smallloanbackenduser.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/loginLog")
public class LogManagerController {
    @Autowired
    private LoginRemote loginRemote;

    //查看登录日志
    @RequestMapping("/getLoginLog")
    public String getLoginLog(Model model,
                              @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum,
                              @RequestParam(defaultValue = "", value = "empName") String empName,
                              @RequestParam(defaultValue = "", value = "startTime") String startTime,
                              @RequestParam(defaultValue = "", value = "endTime") String endTime) {
        //判断条件
        String selEmpName;
        if (!empName.equals("0") && empName != null) {
            selEmpName = empName;
            empName = "%" + empName + "%";
        } else {
            empName = "";
            selEmpName = empName;
        }
        if (startTime.equals("0")) {
            startTime = "";
        }
        if (endTime.equals("0")) {
            endTime = "";
        }
        Page page = loginRemote.getLogin(pageNum, empName, startTime, endTime);
        model.addAttribute("pageInfo", page);
        model.addAttribute("logs", page.getRows());
        model.addAttribute("selEmpName", selEmpName);
        model.addAttribute("startTime", startTime);
        model.addAttribute("endTime", endTime);
        List<Integer> pageArr = new ArrayList<Integer>();
        for (Integer i = 0; i < page.getTotalPageCount(); i++) {
            pageArr.add(i + 1);
        }
        model.addAttribute("pageArr", pageArr);
        return "pages/etao/loginLogManage";
    }

    //带条件查询
    @RequestMapping("/getLoginLogBy")
    @ResponseBody
    public String selectPageBy(Integer pageNum,String empName,String startTime,String endTime) {
        return "/loginLog/getLoginLog?pageNum=" + pageNum + "&empName=" + empName + "&startTime=" + startTime+"&endTime="+endTime;
    }

}
